class Lily

	class Options < Array
		attr_reader :path, :year, :month, :day, :content_flavour
		attr_accessor :flavour

		def initialize(optbase, flavour, env)
			super()
			@path, @flavour, @env = '', flavour, env
			@file_extension = @env['file_extension'].split(' ')
			@content_flavour = 'story'

			self.concat(optbase.split('/')) if optbase
			parse()
		end


		private

		def parse
			cnt = 0
			self.each {|fragment|
				break if fragment =~ /(.+\..+)|\d+$/

				@path << '/' + fragment
				cnt += 1
			}

			if self[cnt] =~ /(.+)\.(.+)$/
				set_path($1, $2)
				cnt += 1
			end

			set_date(cnt) unless cnt == 0

			@path.sub!(/^\/+/, '/')
			@path.sub!(/^\/$/, '')
			indexing()
		end

		def set_path(name, suffix)
			@flavour = suffix

			@file_extension.each {|ext|
				if File.file?("#{@env['datadir']}/#{@path}/#{name}.#{ext}")
					@path << "/#{name}.#{ext}"
					break
				end
			}
		end

		def set_date(cnt)
			@year = self[cnt]
			@month = self[cnt+1]
			@day = self[cnt+2]
		end

		def indexing
			if index?
				@file_extension.each {|ext|
					if File.file?("#{@env['datadir']}/#{@path}/index.#{ext}")
						@content_flavour = 'index'
						@path << "/index.#{ext}"
						self.push("/index.#{ext}")
					end
				}
			end
		end


		public

		# FIXME: foolish
		def join?(day)
			if has_year?
				if @year.to_i == day.year
					if has_month?
						if @month.to_i == day.month
							if has_day?
								if @day.to_i == day.day
									true
								else
									false
								end
							else
								true
							end
						else
							false
						end
					else
						true
					end
				end
			else
				true
			end
		end

		def top?
			[nil, '', '/'].index(@path) && \
				!has_year? && \
				!has_month? && \
				!has_day?
		end

		def index?
			article? ? false : true
		end

		def article?
			return false unless self.last
			self.last.index('.') ? true : false
		end

		def has_year?
			valid?(@year)
		end

		alias :has_date? :has_year?

		def has_month?
			valid?(@month)
		end

		def has_day?
			valid?(@day)
		end


		private

		def valid?(var)
			!(var.nil? || var == '')
		end
	end

end
